﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web_Oto.Business;
using Web_Oto.Common;

namespace Web_Oto
{
    public partial class Services : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                GetService();
        }

        private void GetSeoInformations(string key_descript)
        {
            var seoBus = new SeoBus();
            var seo = seoBus.GetSeoPage("service");
            if (seo != null)
            {
                Page.Title = seo.Title;
                Page.MetaDescription = key_descript + seo.Description;
                Page.MetaKeywords = key_descript + seo.Keyword;
            }
            else
            {
                Page.Title = "Tư vấn dịch vụ";
                Page.MetaDescription = key_descript;
                Page.MetaKeywords = key_descript;
            }
        }

        private void GetService()
        {
            var currentpage = (Page.RouteData.Values.Count > 0 && Page.RouteData.Values.ContainsKey("currentPage"))
                                   ? Convert.ToInt32(Page.RouteData.GetRequiredString("currentPage"))
                                   : 1;
            var itemPerPage = int.Parse(ConfigurationManager.AppSettings["iteminNews"]);
            var totalRecord = 0;
            var serviceBus = new ConsultServiceBus();
            var services = serviceBus.GetAllConsultService(currentpage, itemPerPage, out totalRecord);
            var sb = new StringBuilder();
            var path = ConfigurationManager.AppSettings["servicesPath"];
            var key_descript = "";
            foreach (var service in services)
            {
                sb.Append("<li>");
                sb.Append("<div class='service_intro'>");
                sb.AppendFormat("<a href='/dich-vu-tu-van/{0}/{1}.html'>", CommonHelper.NiceUrl(CommonHelper.RemoveSpecialChars(service.Name)).Replace("--", "-"), service.Id);
                sb.AppendFormat("<img class='left' src='{2}/{0}' alt='{1}' /></a>", service.Image, service.TitleOfImage, path);
                sb.Append("");
                sb.AppendFormat("<a href='/dich-vu-tu-van/{0}/{1}.html'>", CommonHelper.NiceUrl(CommonHelper.RemoveSpecialChars(service.Name)).Replace("--", "-"), service.Id);
                sb.AppendFormat("<div class='content right'><h4>{0}</h4>", service.Name);
                sb.AppendFormat("</a>");
                sb.AppendFormat("<div class='intro'>{0}</div></div>", service.Intro);
                /*sb.AppendFormat("<div class='viewdetail'><a href='/dich-vu-tu-van/{0}/{1}.html'><img src='/images/out_bound_icon.png' title='Chi tiết' alt='read more' /></a></div>", 
                    CommonHelper.NiceUrl(service.Name), service.Id);*/
                sb.Append("</div>");
                sb.Append("<div class='clearfix'></div>");
                sb.Append("</li>");
                key_descript += CommonHelper.NiceUrl(service.Name) + ", " + service.Name + ",";
            }
            GetSeoInformations(key_descript);

            ltrServices.Text = sb.ToString();

            if(totalRecord > 0)
                Paging(currentpage, itemPerPage, totalRecord);
        }

        private void Paging(int currentPage, int itemPerPage, int totalRecord)
        {
            var sb = new StringBuilder();
            int totalPage = (Convert.ToInt32(totalRecord / itemPerPage) == 0) ? 1 :
                Convert.ToInt32(totalRecord / itemPerPage) + 1;
            var currentUrl = Page.Request.Url.AbsolutePath.Split(new char[] { '/' })[1].Replace(".html","");
            int firstPage = 1;
            int lastPage = 1;
            int maxPage = Convert.ToInt32(ConfigurationManager.AppSettings["MaxPage"]);
            firstPage = (currentPage < maxPage) ? (currentPage / maxPage) * maxPage + 1 : (maxPage / currentPage) * maxPage;
            lastPage = (currentPage / maxPage > 0) ? (currentPage / maxPage) * maxPage + maxPage : maxPage;
            if (lastPage > totalPage)
                lastPage = totalPage;
            sb.AppendFormat(currentPage == 1 ? "<li>Đầu</li>" : "<li><a href='/{0}/1.html'>Đầu</a></li>", currentUrl);
            for (int page = firstPage; page <= lastPage; page++)
            {
                sb.AppendFormat(
                    page == currentPage ? "<li class='currentpage'>{0}</li>" : "<li><a href='/{1}/{0}.html'>{0}</a></li>",
                    page, currentUrl);
            }
            sb.AppendFormat(currentPage == totalPage ? "<li>Cuối</li>" : "<li><a href='/{0}/{1}.html'>Cuối</a></li>",
                            currentUrl, totalPage);
            ltrPaging.Text = sb.ToString();
        }
    }
}